<template>
  <div class="dashboard">
    <!-- 欢迎信息 -->
    <div class="welcome-section">
      <el-card class="welcome-card">
        <div class="welcome-content">
          <div class="welcome-text">
            <h2>欢迎回来，{{ userStore.displayName }}！</h2>
            <p>今天是 {{ currentDate }}，祝您工作愉快</p>
          </div>
          <div class="welcome-icon">
            <el-icon size="60" color="#409EFF">
              <Monitor />
            </el-icon>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 统计卡片 -->
    <div class="stats-section">
      <el-row :gutter="20">
        <el-col :xs="12" :sm="6" :md="6" :lg="6">
          <el-card class="stats-card">
            <div class="stats-content">
              <div class="stats-icon patients">
                <el-icon size="24"><UserFilled /></el-icon>
              </div>
              <div class="stats-info">
                <div class="stats-number">{{ stats.totalPatients }}</div>
                <div class="stats-label">总患者数</div>
              </div>
            </div>
          </el-card>
        </el-col>
        
        <el-col :xs="12" :sm="6" :md="6" :lg="6">
          <el-card class="stats-card">
            <div class="stats-content">
              <div class="stats-icon appointments">
                <el-icon size="24"><Calendar /></el-icon>
              </div>
              <div class="stats-info">
                <div class="stats-number">{{ stats.todayAppointments }}</div>
                <div class="stats-label">今日预约</div>
              </div>
            </div>
          </el-card>
        </el-col>
        
        <el-col :xs="12" :sm="6" :md="6" :lg="6">
          <el-card class="stats-card">
            <div class="stats-content">
              <div class="stats-icon checkups">
                <el-icon size="24"><Monitor /></el-icon>
              </div>
              <div class="stats-info">
                <div class="stats-number">{{ stats.completedCheckups }}</div>
                <div class="stats-label">已完成体检</div>
              </div>
            </div>
          </el-card>
        </el-col>
        
        <el-col :xs="12" :sm="6" :md="6" :lg="6">
          <el-card class="stats-card">
            <div class="stats-content">
              <div class="stats-icon reports">
                <el-icon size="24"><Document /></el-icon>
              </div>
              <div class="stats-info">
                <div class="stats-number">{{ stats.pendingReports }}</div>
                <div class="stats-label">待审核报告</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 主要内容区域 -->
    <el-row :gutter="20" class="main-content">
      <!-- 今日预约 -->
      <el-col :xs="24" :sm="24" :md="12" :lg="12">
        <el-card class="content-card">
          <template #header>
            <div class="card-header">
              <span>今日预约</span>
              <el-button type="text" @click="$router.push('/appointments')">
                查看全部
              </el-button>
            </div>
          </template>
          
          <div v-if="todayAppointments.length === 0" class="empty-state">
            <el-empty description="今日暂无预约" :image-size="80" />
          </div>
          
          <div v-else class="appointment-list">
            <div
              v-for="appointment in todayAppointments"
              :key="appointment.id"
              class="appointment-item"
            >
              <div class="appointment-time">
                {{ appointment.time }}
              </div>
              <div class="appointment-info">
                <div class="patient-name">{{ appointment.patientName }}</div>
                <div class="package-name">{{ appointment.packageName }}</div>
              </div>
              <div class="appointment-status">
                <el-tag :type="getStatusType(appointment.status)" size="small">
                  {{ getStatusText(appointment.status) }}
                </el-tag>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <!-- 快速操作 -->
      <el-col :xs="24" :sm="24" :md="12" :lg="12">
        <el-card class="content-card">
          <template #header>
            <span>快速操作</span>
          </template>
          
          <div class="quick-actions">
            <div class="action-grid">
              <div class="action-item" @click="$router.push('/patients')">
                <el-icon size="32" color="#409EFF"><UserFilled /></el-icon>
                <span>患者管理</span>
              </div>
              
              <div class="action-item" @click="$router.push('/appointments')">
                <el-icon size="32" color="#67C23A"><Calendar /></el-icon>
                <span>预约管理</span>
              </div>
              
              <div class="action-item" @click="$router.push('/checkups')">
                <el-icon size="32" color="#E6A23C"><Monitor /></el-icon>
                <span>体检管理</span>
              </div>
              
              <div class="action-item" @click="$router.push('/reports')">
                <el-icon size="32" color="#F56C6C"><Document /></el-icon>
                <span>报告管理</span>
              </div>
              
              <div v-if="userStore.isAdmin" class="action-item" @click="$router.push('/users')">
                <el-icon size="32" color="#909399"><User /></el-icon>
                <span>用户管理</span>
              </div>
              
              <div class="action-item" @click="$router.push('/profile')">
                <el-icon size="32" color="#606266"><Setting /></el-icon>
                <span>个人设置</span>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 系统公告 -->
    <el-row class="announcement-section">
      <el-col :span="24">
        <el-card class="content-card">
          <template #header>
            <div class="card-header">
              <span>系统公告</span>
            </div>
          </template>
          
          <div class="announcement-list">
            <div
              v-for="announcement in announcements"
              :key="announcement.id"
              class="announcement-item"
            >
              <div class="announcement-content">
                <div class="announcement-title">{{ announcement.title }}</div>
                <div class="announcement-summary">{{ announcement.summary }}</div>
              </div>
              <div class="announcement-date">{{ announcement.date }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useUserStore } from '@/store/user.store'

const userStore = useUserStore()

// 响应式数据
const stats = ref({
  totalPatients: 1248,
  todayAppointments: 23,
  completedCheckups: 156,
  pendingReports: 8
})

const todayAppointments = ref([
  {
    id: 1,
    time: '09:00',
    patientName: '张三',
    packageName: '基础体检套餐',
    status: 'confirmed'
  },
  {
    id: 2,
    time: '10:30',
    patientName: '李四',
    packageName: '全面体检套餐',
    status: 'pending'
  },
  {
    id: 3,
    time: '14:00',
    patientName: '王五',
    packageName: '标准体检套餐',
    status: 'in_progress'
  }
])

const announcements = ref([
  {
    id: 1,
    title: '系统维护通知',
    summary: '系统将于本周六凌晨2:00-4:00进行维护升级，期间可能影响正常使用。',
    date: '2024-01-15'
  },
  {
    id: 2,
    title: '新功能上线',
    summary: '体检报告在线查看功能已上线，患者可通过微信小程序查看报告。',
    date: '2024-01-10'
  },
  {
    id: 3,
    title: '节假日安排',
    summary: '春节期间（2月10日-17日）体检中心正常营业，请提前预约。',
    date: '2024-01-08'
  }
])

// 计算属性
const currentDate = computed(() => {
  const now = new Date()
  const options = {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'long'
  }
  return now.toLocaleDateString('zh-CN', options)
})

// 方法
const getStatusType = (status) => {
  const statusMap = {
    pending: 'warning',
    confirmed: 'success',
    in_progress: 'primary',
    completed: 'info',
    cancelled: 'danger'
  }
  return statusMap[status] || 'info'
}

const getStatusText = (status) => {
  const statusMap = {
    pending: '待确认',
    confirmed: '已确认',
    in_progress: '进行中',
    completed: '已完成',
    cancelled: '已取消'
  }
  return statusMap[status] || '未知'
}

// 生命周期
onMounted(() => {
  // 这里可以调用API获取实际的统计数据
  // loadDashboardData()
})
</script>

<style scoped>
.dashboard {
  padding: 0;
}

.welcome-section {
  margin-bottom: 20px;
}

.welcome-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
}

.welcome-card :deep(.el-card__body) {
  padding: 30px;
}

.welcome-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.welcome-text h2 {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
}

.welcome-text p {
  margin: 0;
  opacity: 0.9;
  font-size: 14px;
}

.stats-section {
  margin-bottom: 20px;
}

.stats-card {
  height: 100px;
}

.stats-card :deep(.el-card__body) {
  padding: 20px;
  height: 100%;
  display: flex;
  align-items: center;
}

.stats-content {
  display: flex;
  align-items: center;
  width: 100%;
}

.stats-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  color: white;
}

.stats-icon.patients {
  background: linear-gradient(135deg, #667eea, #764ba2);
}

.stats-icon.appointments {
  background: linear-gradient(135deg, #f093fb, #f5576c);
}

.stats-icon.checkups {
  background: linear-gradient(135deg, #4facfe, #00f2fe);
}

.stats-icon.reports {
  background: linear-gradient(135deg, #43e97b, #38f9d7);
}

.stats-info {
  flex: 1;
}

.stats-number {
  font-size: 24px;
  font-weight: 600;
  color: #2c3e50;
  line-height: 1;
}

.stats-label {
  font-size: 12px;
  color: #7f8c8d;
  margin-top: 4px;
}

.main-content {
  margin-bottom: 20px;
}

.content-card {
  height: 400px;
}

.content-card :deep(.el-card__body) {
  height: calc(100% - 60px);
  overflow-y: auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.empty-state {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.appointment-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.appointment-item {
  display: flex;
  align-items: center;
  padding: 12px;
  background: #f8f9fa;
  border-radius: 8px;
  transition: all 0.3s;
}

.appointment-item:hover {
  background: #e3f2fd;
  transform: translateY(-1px);
}

.appointment-time {
  font-weight: 600;
  color: #409EFF;
  min-width: 60px;
}

.appointment-info {
  flex: 1;
  margin-left: 12px;
}

.patient-name {
  font-weight: 500;
  color: #2c3e50;
  margin-bottom: 4px;
}

.package-name {
  font-size: 12px;
  color: #7f8c8d;
}

.appointment-status {
  margin-left: 12px;
}

.quick-actions {
  height: 100%;
  display: flex;
  align-items: center;
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  width: 100%;
}

.action-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s;
  text-align: center;
}

.action-item:hover {
  background: #e3f2fd;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.action-item span {
  margin-top: 8px;
  font-size: 12px;
  color: #606266;
  font-weight: 500;
}

.announcement-section {
  margin-bottom: 20px;
}

.announcement-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.announcement-item {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border-left: 4px solid #409EFF;
}

.announcement-content {
  flex: 1;
}

.announcement-title {
  font-weight: 500;
  color: #2c3e50;
  margin-bottom: 4px;
}

.announcement-summary {
  font-size: 14px;
  color: #7f8c8d;
  line-height: 1.5;
}

.announcement-date {
  font-size: 12px;
  color: #bdc3c7;
  margin-left: 16px;
  white-space: nowrap;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .welcome-content {
    flex-direction: column;
    text-align: center;
  }
  
  .welcome-icon {
    margin-top: 16px;
  }
  
  .action-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }
  
  .action-item {
    padding: 16px 12px;
  }
  
  .announcement-item {
    flex-direction: column;
    gap: 8px;
  }
  
  .announcement-date {
    margin-left: 0;
    align-self: flex-end;
  }
}

@media (max-width: 480px) {
  .stats-number {
    font-size: 20px;
  }
  
  .stats-label {
    font-size: 11px;
  }
  
  .action-grid {
    grid-template-columns: 1fr;
  }
}
</style>